package com.example.denglu.DengLu;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.denglu.App;
import com.example.denglu.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LengLu extends AppCompatActivity {
    private EditText zh;
    private EditText mima;
    private CheckBox checkZddl;
    private CheckBox checkJzmm;
    private Button but;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lenglu);
        initView();
        setinput();
        oncikebut();
    }

    private void setinput() {
        SharedPreferences usescookie = getSharedPreferences("usescookie", LengLu.MODE_PRIVATE);
        zh.setText(usescookie.getString("uses",""));
        mima.setText(usescookie.getString("password",""));
        checkJzmm.setChecked(usescookie.getBoolean("checked_mm",false));
        checkZddl.setChecked(usescookie.getBoolean("checkZddl",false));

        if (usescookie.getBoolean("checkZddl",false)){
            postHttp(usescookie.getString("uses",""),usescookie.getString("password",""),usescookie.getBoolean("checked_mm",false));
        }
    }

    private void oncikebut() {
        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = zh.getText().toString();
                String s1 = mima.getText().toString();
                boolean checked_dl = checkZddl.isChecked();
                boolean checked_mm = checkJzmm.isChecked();

                SharedPreferences usescookie = getSharedPreferences("usescookie", LengLu.MODE_PRIVATE);
                SharedPreferences.Editor edit = usescookie.edit();

                if (checked_dl == true) {
                    edit.putBoolean("checkZddl",checked_dl).commit();
                }else {
                    edit.remove("checkZddl").commit();
                }
                if (checked_mm == true) {
                    edit.putBoolean("checked_mm",checked_mm).commit();
                }else {
                    edit.remove("checked_mm").commit();
                }

                postHttp(s, s1,checked_mm);
            }
        });
    }


    private void postHttp(String s, String s1,boolean checked_mm) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("username", s);
                    jsonObject.put("password", s1);
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
                OkHttpClient client = new OkHttpClient().newBuilder()
                        .build();
                MediaType mediaType = MediaType.parse("application/json");
                RequestBody body = RequestBody.create(mediaType, jsonObject.toString());
                Request request = new Request.Builder()
                        .url("http://124.93.196.45:10001/prod-api/api/login")
                        .method("POST", body)
                        .addHeader("Content-Type", "application/json")
                        .build();
                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
                        Log.d("TAG", "onFailure: 失败");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                            }
                        });
                    }

                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        String string = response.body().string();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                SharedPreferences usescookie = getSharedPreferences("usescookie", LengLu.MODE_PRIVATE);
                                SharedPreferences.Editor edit = usescookie.edit();
                                edit.putString("uses",s);
                                if (checked_mm){
                                    edit.putString("password",s1);
                                    edit.commit();
                                }else {
                                    edit.remove("password");
                                    edit.commit();
                                }
                                try {
                                    JSONObject CookiejsonObject = new JSONObject(string);
                                    int code = CookiejsonObject.getInt("code");

                                    if (code==200) {
                                        String token = CookiejsonObject.getString("token");
                                        edit.putString("token",token).commit();
                                        finish();
                                    } else {

                                    }
                                } catch (JSONException e) {
                                    throw new RuntimeException(e);
                                }
                            }
                        });
                    }
                });
                try {
                    Response response = client.newCall(request).execute();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();

    }

    private void initView() {
        zh = findViewById(R.id.zh);
        mima = findViewById(R.id.mima);
        checkZddl = findViewById(R.id.checkZddl);
        checkJzmm = findViewById(R.id.checkJzmm);
        but = findViewById(R.id.but);

    }
}